package com.cskaoyan.wordmemorize.service.impl;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.cskaoyan.wordmemorize.common.constant.ResultCodeEnum;
import com.cskaoyan.wordmemorize.common.exception.BusinessException;
import com.cskaoyan.wordmemorize.dto.admin.VocExcelDTO;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class VocExcelListener extends AnalysisEventListener<VocExcelDTO> {

    private List<VocExcelDTO> dataList = new ArrayList<>();

    @Override
    public void invoke(VocExcelDTO data, AnalysisContext context) {
        // 基础校验
        if (data.getContent() == null || data.getContent().trim().isEmpty()) {
            throw new BusinessException(ResultCodeEnum.VOC_EXCEL_DATA_ERROR);
        }

        dataList.add(data);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        log.info("Excel文件读取完成，共读取 {} 条数据", dataList.size());
        if (dataList.isEmpty()) {
            throw new BusinessException(ResultCodeEnum.VOC_EXCEL_TEMPLATE_READ_ERROR);
        }
    }

    public List<VocExcelDTO> getDataList() {
        return dataList;
    }
}